/**
 * ReportGear(2011)
 */
package com.reportgear.core.data;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;

import netscape.javascript.JSObject;

import com.reportgear.core.data.model.SalaryDataObject;
import com.reportgear.core.xml.Dom4jUtils;
import com.reportgear.report.model.cell.value.salary.SalaryObjectType;
import com.reportgear.report.setting.AppletSettings;
import com.sot.core.util.NumberUtils;
import com.sot.core.util.StringUtils;

/**
 * 通过Js查询薪酬数据,Applet使用本方式
 * 
 * @version 1.0 2011-5-28
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class SalaryDataQueryXmlByJs extends AbstractSalaryDataQueryXml {
	@Override
	public List<SalaryDataObject> getOrgs() {
		return this.xmlDataToList(this.query("GetOrgs.ashx", new Object[0]));
	}

	@Override
	public List<SalaryDataObject> getCenters() {
		return this.xmlDataToList(this.query("GetCenters.ashx", new Object[0]));
	}

	@Override
	public List<SalaryDataObject> getSalaryScheme() {
		return this.xmlDataToList(this.query("GetSalarySystems.ashx", new Object[0]));
	}

	@Override
	public List<SalaryDataObject> getSalaryItem(int schemeId) {
		return this.xmlDataToList(this.query("GetSalaryItemsBySysID.ashx", new Object[] { "ID", schemeId }));
	}

	@Override
	public List<SalaryDataObject> getPayTotal(int payId, SalaryObjectType objectType, int level, Set<Integer> rowSet,
			Set<Integer> colSet) {
		return this.xmlDataToList(this.xmlQuery("GetQueryDataByTemplate.ashx", this.genQueryXml(payId, objectType,
				level, rowSet, colSet), new Object[0]));
	}

	@Override
	public List<SalaryDataObject> getPayDoc(int paymentId, SalaryObjectType objectType, int level,
			Set<Integer> rowSet, Set<Integer> colSet) {
		return this.xmlDataToList(this.xmlQuery("GetQueryDocByTemplate.ashx", this.genQueryXml(paymentId, objectType,
				level, rowSet, colSet), new Object[0]));
	}

	@Override
	public List<SalaryDataObject> getPayDetail(int paymentId, SalaryObjectType objectType, int level,
			Set<Integer> rowSet, Set<Integer> colSet) {
		return this.xmlDataToList(this.xmlQuery("GetQueryDetailByTemplate.ashx", this.genQueryXml(paymentId,
				objectType, level, rowSet, colSet), new Object[0]));
	}

	@Override
	public String loadTemplate(int reportId) {
		return this.query("GetTemplateByID.ashx", new Object[] { "ID", reportId });
	}

	@Override
	public List<SalaryDataObject> loadTemplateList() {
		return this.xmlDataToList(this.query("GetTemplates.ashx", new Object[0]));
	}

	@Override
	public int saveTemplate(String template, int id, int SystemId, String name) {
		return NumberUtils.convertToInteger(this.xmlQuery("SaveTemplate.ashx?ID={0}&SystemID={1}&Name={2}", template,
				new Object[] { id, SystemId, name }), 0);
	}

	@Override
	public boolean deleteTemplate(int id) {
		return !StringUtils.isEmpty(this.query("DeleteTemplateByID.ashx", new Object[] { "ID", id }));
	}

	@Override
	public List<SalaryDataObject> getPayObjects(int paymentId, SalaryObjectType objectType, int level,
			Set<Integer> rowSet, Set<Integer> colSet) {
		return this.xmlDataToList(this.xmlQuery("GetObjectsByPID.ashx", this.genQueryXml(paymentId, objectType, level,
				rowSet, colSet), new Object[0]));
	}

	@Override
	public List<SalaryDataObject> getOrderInfo(String key) {
		return this.xmlDataToList(this.query("GetSalaryQuerySettingInfo.ashx", new Object[] { "key", key }));
	}

	@Override
	public int saveOrderInfo(String key, String content) {
		return NumberUtils.convertToInteger(this.xmlQuery("SaveSalaryQuerySettingInfo.ashx?key={0}", content,
				new Object[] { key }), 0);
	}

	@Override
	public List<SalaryDataObject> getLastPayments() {
		return this.xmlDataToList(this.query("GetSalaryPayInfos.ashx", new Object[0]));
	}

	@Override
	public SalaryDataObject getSessionVar() {
		return this.xmlRootToList(this.query("GetUserInfo.ashx", new Object[0]));
	}

	@Override
	public SalaryDataObject getPayBase(int payId) {
		String xml = this.query("GetSalaryPayInfo.ashx", new Object[] { "ID", payId });

		if (StringUtils.isEmpty(xml)) {
			return new SalaryDataObject();
		} else {
			Document doc = Dom4jUtils.createDocument(xml);
			Element root = doc.getRootElement();

			SalaryDataObject sdo = new SalaryDataObject();
			for (Object ao : root.attributes()) {
				Attribute aa = (Attribute) ao;

				sdo.setValue(aa.getName(), StringUtils.trim(aa.getValue()));
			}

			return sdo;
		}
	}

	/**
	 * XML查询
	 * 
	 * @param command
	 *            查询地址
	 * @param queryXml
	 *            xml文档
	 * @param params
	 *            参数
	 * @return 查询返回结果
	 */
	private String xmlQuery(String command, String queryXml, Object[] params) {
		String result = "";
		int paramCount = (params == null) ? 2 : params.length + 2;

		// 组装参数
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(command);
		paramList.add(queryXml);
		for (Object o : params) {
			paramList.add(o);
		}

		JSObject jso = AppletSettings.INSTANCE.getJsObject();
		if (jso != null) {
			result = (String) jso.call("xmlQuery", paramList.toArray());
		}

		return result;
	}

	/**
	 * 参数查询
	 * 
	 * @param command
	 *            查询地址
	 * @param params
	 *            查询参数
	 * @return 查询结果
	 */
	private String query(String command, Object[] params) {
		String result = "";
		int paramCount = (params == null) ? 2 : params.length + 2;

		// 组装参数
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(command);
		for (Object o : params) {
			paramList.add(o);
		}

		JSObject jso = AppletSettings.INSTANCE.getJsObject();
		if (jso != null) {
			result = (String) jso.call("query", paramList.toArray());
		}
		return result;
	}

	/**
	 * 返回查询请求XML
	 * 
	 * @param payId
	 *            发放表Id
	 * @param objectType
	 *            薪酬对象类型
	 * @param level
	 *            查询层级
	 * @param rowSet
	 *            行数据集
	 * @param colSet
	 *            列数据集
	 * @return XML
	 */
	private String genQueryXml(int payId, SalaryObjectType objectType, int level, Set<Integer> rowSet,
			Set<Integer> colSet) {
		StringBuilder sb = new StringBuilder();
		sb.append("<Root>");

		sb.append(String.format("<Header queryType=\"%d\" paymentId=\"%d\" level=\"%d\"/>", objectType.getValue(),
				payId, level));

		sb.append("<Cols>");
		for (int id : colSet) {
			sb.append(String.format("<Col id=\"%d\"/>", id));
		}
		sb.append("</Cols>");

		sb.append("<Rows>");
		for (int id : rowSet) {
			sb.append(String.format("<Row id=\"%d\"/>", id));
		}
		sb.append("</Rows>");
		sb.append("</Root>");

		return sb.toString();
	}
}
